<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Print </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><a class='wikilink' href='Serial.html'>Serial</a>
</p>
<p class='vspace'></p><h2>print()</h2>
<h3>Description</h3>
<p>Prints data to the serial port as human-readable ASCII text.  This command can take many forms.  Numbers are printed using an ASCII character for each digit.  Floats are similarly printed as ASCII digits, defaulting to two decimal places.  Bytes are sent as a single character.  Characters and strings are sent as is. For example:
</p><ul><li>Serial.print(78) gives "78"
</li><li>Serial.print(1.23456) gives "1.23"
</li><li>Serial.print('N') gives "N"
</li><li>Serial.print("Hello world.") gives "Hello world."
</li></ul><p class='vspace'></p><p>An optional second parameter specifies the base (format) to use; permitted values are BIN (binary, or base 2), OCT (octal, or base 8), DEC (decimal, or base 10), HEX (hexadecimal, or base 16). For floating point numbers, this parameter specifies the number of decimal places to use.  For example:
</p><ul><li>Serial.print(78, BIN) gives "1001110"
</li><li>Serial.print(78, OCT) gives "116"
</li><li>Serial.print(78, DEC) gives "78" 
</li><li>Serial.print(78, HEX) gives "4E"
</li><li>Serial.println(1.23456, 0) gives "1"
</li><li>Serial.println(1.23456, 2) gives "1.23"
</li><li>Serial.println(1.23456, 4) gives "1.2346"
</li></ul><p class='vspace'></p><p>You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
</p><ul><li>Serial.print(F(“Hello World”))
</li></ul><p class='vspace'></p><p>To send a single byte, use  <a class='wikilink' href='Serial_Write.html'>Serial.write</a>().
</p>
<p class='vspace'></p><h3>Syntax</h3>
<p>Serial.print(val) <br />Serial.print(val, format)
</p>
<p class='vspace'></p><h3>Parameters</h3>
<p>val: the value to print - any data type
</p>
<p class='vspace'></p><p>format: specifies the number base (for integral data types) or number of decimal places (for floating point types)
</p>
<p class='vspace'></p><h3>Returns</h3>
<p>byte<br />print() will return the number of bytes written, though reading that number is optional
</p>
<p class='vspace'></p><h3>Example:</h3>
<pre>
/*
  Uses a FOR loop for data and prints a number in various formats.
*/
int x = 0;    // variable

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:    
}

void loop() {  
  // print labels 
  Serial.print("NO FORMAT");       // prints a label
  Serial.print("\t");              // prints a tab

  Serial.print("DEC");  
  Serial.print("\t");      

  Serial.print("HEX"); 
  Serial.print("\t");   

  Serial.print("OCT");
  Serial.print("\t");

  Serial.print("BIN");
  Serial.print("\t"); 

  for(x=0; x< 64; x++){    // only part of the ASCII chart, change to suit

    // print it out in many formats:
    Serial.print(x);       // print as an ASCII-encoded decimal - same as "DEC"
    Serial.print("\t");    // prints a tab

    Serial.print(x, DEC);  // print as an ASCII-encoded decimal
    Serial.print("\t");    // prints a tab

    Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal
    Serial.print("\t");    // prints a tab

    Serial.print(x, OCT);  // print as an ASCII-encoded octal
    Serial.print("\t");    // prints a tab

    Serial.println(x, BIN);  // print as an ASCII-encoded binary
    //                             then adds the carriage return with "println"
    delay(200);            // delay 200 milliseconds
  }
  Serial.println("");      // prints another carriage return
}
</pre>
<p class='vspace'></p><h4>Programming Tips </h4>
<p>As of version 1.0, serial transmission is asynchronous; Serial.print() will return before any characters are transmitted.
</p>
<p class='vspace'></p><h4>See also</h4>
<ul><li><a class='wikilink' href='Serial_Write.html'>Serial.write</a>()
</li><li><a class='wikilink' href='Serial_Println.html'>Serial.println</a>()
</li><li><a class='wikilink' href='Tutorial_Memory.html'>Memory</a>
</li></ul><p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia getting started guide is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on Arduino reference.  Code samples in the guide are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='Serial_Print?action=edit.html'>Edit Page</a> | <a href='Serial_Print?action=diff.html'>Page History</a> | <a href='Serial_Print?action=print.html' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
